<?php
/**
 * 临时系统
 */

class foodModel extends model
{
	public function user_add($user_name,$password){
		$password = md5($password);
		$data = array('user_name' => $user_name, 'password' => $password, 'city' => 'beijing', 'sex' => 1
				,'age' => 18, 'height' => 170, 'weight' => 60);
		$user_id = DB::insert('food_user', $data);
		Cookie::set('food_user_id', $user_id);
		Cookie::set('food_user_name', $user_name);
		return $user_id;
	}
	
	public function user_modify($user_id,$city,$sex,$age,$height,$weight){
		$data = array('city' => $city, 'sex' => $sex
				,'age' => $age, 'height' => $height, 'weight' => $weight);
		$condition = "user_id=$user_id";
		$row = DB::update('food_user',$data,$condition);
		if($row>0) return 1;
		else return 0;
	}
	
	public function user_set_password($user_id,$password){
		$password = $this->password($password);
		$data = array('password' => $password);
		$condition = "user_id=$user_id";
		$row = DB::update('food_user',$data,$condition);
		if($row>0) return 1;
		else return 0;
	}
	
	public function user_get_count_by_user_name($user_name){
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('food_user')." where user_name='$user_name'");
		return $row['num'];
	}
	
	public function user_login($user_name,$password){
		$password = md5($password);
		$row = DB::fetchOne('SELECT * FROM '.DB::table('food_user').' WHERE user_name = "'.DB::escape_string($user_name).'"');
		if(count($row) == 0){
			return -2;	//用户不存在
		}
		if($row['password'] != $password){
			return -3;	//密码错误
		}
		//设置COOKIE
		Cookie::set('food_user_id', $row['user_id']);
		Cookie::set('food_user_name', $row['user_name']);
		return 1;
	}
	
	public function user_logout(){
		Cookie::set('food_user_id', null);
		Cookie::set('food_user_name', null);
		return 1;
	}
	public function user_get_by_id($user_id){
		$row = DB::fetchOne('SELECT * FROM '.DB::table('food_user').' WHERE user_id = '.DB::escape_string($user_id));
		if(count($row) == 0){
			return array();
		}
		return $row;
	}
	
	
	public function menu_add(){
		
	}
	
		
	public function menu_get_by_id($menu_id){
		$row = DB::fetchOne('SELECT * FROM '.DB::table('food_menu').' WHERE menu_id = '.$menu_id);
		if(count($row) == 0){
			return array();
		}
		return $row;
	}
	
	public function menu_get_top_one($city){
		$weather = $this->get_weather_by_city($city);//当前天气
		$temperature=0;		//根据city获得
		$humidity=0;		//根据city获得
		
		$condition = " WHERE (fit_temperature=$temperature OR fit_temperature=0) AND (fit_humidity=$humidity OR fit_humidity=0) ";
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_menu')." $condition limit 1");
		return $r[0];
	}
	
	public function menu_get($pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('food_menu'));
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_menu')." limit $limitstart,$pagesize");
		return $r;
	}
	public function menu_get_by_score($count){
		if($count<=0) return array();
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_menu')." ORDER by score desc limit $count");
		return $r;
	}
	
	//一级搜索 根据当地城市（天气）来搜
	public function menu_search_1($city,$pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		$weather = $this->get_weather_by_city($city);//当前天气
		$temperature=0;		//根据city获得
		$humidity=0;		//根据city获得
		
		$condition = " WHERE (fit_temperature=$temperature OR fit_temperature=0) AND (fit_humidity=$humidity OR fit_humidity=0) ";
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('food_menu')." $condition");
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_menu')." $condition limit $limitstart,$pagesize");
		return $r;
	}
	
	//二级搜索 根据未登录用户筛选信息搜索
	public function menu_search_2($sex,$age,$city,$pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		$weather = $this->get_weather_by_city($city);//当前天气
		$temperature=0;		//根据city获得
		$humidity=0;		//根据city获得
		
		$condition = " WHERE (fit_temperature=$temperature OR fit_temperature=0) AND (fit_humidity=$humidity OR fit_humidity=0) ";
		$age>0 && $condition.=" AND (fit_age_down<$age AND $age<fit_age_up) ";
		$sex>0 && $condition.=" AND (fit_sex=$sex OR fit_sex=0) ";
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('food_menu')." $condition");
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_menu')." $condition limit $limitstart,$pagesize");
		return $r;
	}
	
	//三级搜索 根据用户注册信息搜索
	public function menu_search_3($user_id,$pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		$user = DB::fetchOne('SELECT * FROM '.DB::table('food_user').' WHERE user_id = '.$user_id);
		$city = $user['city'];
		$sex = $user['sex'];
		$age = $user['age'];
		$height = $user['height'];
		$weight = $user['weight'];
		$state = $user['state'];
		$weather = $this->get_weather_by_city($city);//当前天气
		$health=0;			//根据state获得
		$figure=0;			//根据height与weight获得
		$temperature=0;		//根据city获得
		$humidity=0;		//根据city获得
		
		
		$condition = " WHERE (fit_temperature=$temperature OR fit_temperature=0) AND (fit_humidity=$humidity OR fit_humidity=0) AND (fit_health=$health OR fit_health=0) ";
		$age>0 && $condition.=" AND (fit_age_down<$age AND $age<fit_age_up) ";
		$sex>0 && $condition.=" AND (fit_sex=$sex OR fit_sex=0) ";
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('food_menu')." $condition");
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_menu')." $condition limit $limitstart,$pagesize");
		return $r;
	}
	
	public function menu_search_by_keyword($keyword,$pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		
		$condition = " WHERE subject like '%$keyword%' ";
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('food_menu')." $condition");
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_menu')." $condition limit $limitstart,$pagesize");
		return $r;
	}
	
	//添加评论
	public function comment_add($user_id,$menu_id,$message,$score){
		$data = array('message' => $message,'score' => intval($score),'user_id' => $user_id,'menu_id' => $menu_id,'mktime'=>time());
		$comment_id = DB::insert('food_comment', $data);
		//更新menu得分
		if($score>0){
			DB::query("update ".DB::table('food_menu')." set score=score+$score WHERE menu_id=$menu_id");
		}
		return $comment_id;
	}
	
	public function comment_get($menu_id,$pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('food_comment')." WHERE menu_id=$menu_id");
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_comment')." AS fm,".DB::table('food_user')." AS fu WHERE fm.user_id=fu.user_id AND fm.menu_id=$menu_id ORDER by mktime desc limit $limitstart,$pagesize");
		return $r;
	}
	
	public function comment_get_new($count){
		if($count<=0) return array();
		$r = DB::fetchArray("SELECT * FROM ".DB::table('food_comment')." AS fm,".DB::table('food_user')." AS fu WHERE fm.user_id=fu.user_id  ORDER by mktime desc limit $count");
		return $r;
	}
	
	
	
	public function get_tip_by_city($city){
		$weather = $this->get_weather_by_city($city);
		
		$tip="今天天气不错，来杯咖啡如何？";
		return $tip;
	}
	
	public function get_ldate(){
		$curdate=date("Y-m-d");
		$date_elements = explode("-" ,$curdate);
		@include_once ROOT.'source/lib/date/lunardate.php';
		$lunar = new Lunar();
		$ldate=$lunar->convertSolarToLunar($date_elements[0], $date_elements[1], $date_elements[2]);
		return $ldate;
	}
	
	public function message_out($msg){
		echo "<script>alert('$msg')</script>";
	}
	
	
	private function get_weather_by_city($city='beijing'){
		//这个地方根据时间写个缓存
		return array();
		
		$curdate=date("Y-m-d");
		global $WEATHWER;
		if(!empty($WEATHWER[$curdate][$city]))
			return $WEATHWER[$curdate][$city];
		
		$url ="http://www.google.com/ig/api?weather=$city&hl=zh-cn";
		$content = file_get_contents($url);
		$content || die("No such city's data");
		$content = mb_convert_encoding($content, 'UTF-8', 'GBK');
		$xml = simplexml_load_string($content);
		$date = $xml->weather->forecast_information->forecast_date->attributes();
		$time = date("Y-m-d H:i:m");
		$current = $xml->weather->current_conditions;
		$condition = $current->condition->attributes();
		$temp_c = $current->temp_c->attributes();
		$humidity = $current->humidity->attributes();
		$wind=$current->wind_condition->attributes();
		$icon = $current->icon->attributes();
		$icon && $icon = $xml->weather->forecast_conditions->icon->attributes();
		//预告部分
		/*
		foreach($xml->weather->forecast_conditions as $forecast) {
			$low = $forecast->low->attributes();
			$high = $forecast->high->attributes();
			$icon = $forecast->icon->attributes();
			$condition = $forecast->condition->attributes();
			$day_of_week = $forecast->day_of_week->attributes();
		}
		*/
		$wthr=array('date'=>$date,'time'=>$time,'temp_c'=>$temp_c,'humidity'=>$humidity,'wind'=>$wind);
		$WEATHWER[$curdate][$city]=$wthr;
		return $wthr;
	}
}
?>